#ifndef _PRESENTATION_PDF_HPP_
#define _PRESENTATION_PDF_HPP_

// presentation swf type

#include "presentation.hpp"
#include "generator.hpp"
#include <cairo.h>
#include <cairo-pdf.h>

#include "gen_text_pdf.hpp"
#include "gen_figure_pdf.hpp"
#include "gen_video_pdf.hpp"
#include "gen_inline_pdf.hpp"

class presentation_pdf:public xlider_presentation {
public:
	presentation_pdf(string pf):xlider_presentation(pf){}

	string convert(string input_file, string oname){
		string gf=input_file;
		string fl=(oname+"-convert.png");

		if(file_type(gf,"eps")) {
			call_script("convert-eps", fl+" "+gf+ " flat '"+background_color+"'");
			garbage.push_back(fl);
		} else if(file_type(gf,"jpg")) {
			call_script("convert-jpg", fl+" "+gf+" flat '"+background_color+"'");
			garbage.push_back(fl);
		} else if(file_type(gf,"png")) {
			call_script("convert-png", fl+" "+gf+" flat '"+background_color+"'");
			garbage.push_back(fl);
		} else {fl=gf;}
		assert(file_type(fl,"pdf")||file_type(fl,"png"), "unsuported figure file tipe", gf);
		assert(file_exist(fl), "failed to convert", gf);

		if(file_exist(fl+".info")){
			garbage.push_back(fl+".info");
		}
		return fl;
	}	

	void assign_generators() {
		for(int i=0;i<item.size();i++) {
			if(item[i]->type=="text") {
				item[i]->set_generator(new text_pdf); continue;
			}
			
			if(item[i]->type=="figure"){
				item[i]->set_generator(new figure_pdf); continue;
			}
			
			if(item[i]->type=="movie"){
				item[i]->set_generator(new video_pdf); continue;
			}

			if(item[i]->type=="inline"){
				item[i]->set_generator(new inline_pdf); continue;
			}
		
		}
	}
	
	void create_slide(){
		double r,g,b;
		assign_generators();

        cairo_surface_t *surface;
        cairo_surface_t *img;
        cairo_t *cr;
        

		string slidepdf=slide_names[slide_idx]+".pdf";

		// FIXME! must be normalized to A4
        surface = cairo_pdf_surface_create (slidepdf.c_str(), slide_width, slide_height);
        cr = cairo_create (surface);

		convert_color(background_color,r,g,b);
		cairo_rectangle(cr, 0.0, 0.0, slide_width, slide_height);
		cairo_set_source_rgb(cr, r,g,b);
		cairo_set_line_width(cr, 0.0);
		cairo_fill(cr);
		cairo_stroke(cr);
		
		// WARNING! no transparency in pdf!
		if(background_image!=""){
			string bgimg=convert(get_imagepath(background_image), slide_names[slide_idx]);
			DEBUGSTR("background", get_imagepath(background_image).c_str());
			// cairo accepts only png
			cairo_scale(cr, background_scale, background_scale);
			img=cairo_image_surface_create_from_png(bgimg.c_str());
			cairo_set_source_surface(cr, img, 0, 0);
			cairo_paint(cr);
			cairo_scale(cr, 1.0/background_scale, 1.0/background_scale);
		}
		
		// create items
		double nexttop=0.0;
		for(int i=0;i<item.size();i++){
			double ind=margin_left+item[i]->get_indent();
			double upd=margin_top+nexttop;
			if(!item[i]->floating)item[i]->set_pos(ind,upd);
			item[i]->generate(cr);
			if(!item[i]->floating)nexttop+=item[i]->get_height();
		}

		cairo_destroy(cr);
    	cairo_surface_show_page(surface);
    	cairo_surface_finish(surface);
		cairo_surface_destroy(surface);
		
		for(int i=0;i<item.size();i++) item[i]->clean_garbage();
		garbage.push_back(slidepdf);
	}

	void combine_slides(){
		// use gs!!! maybe writen in script
//		string combine_cmd("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=");
		string combine_opt(basename(filename,true)+".pdf ");
		int totslide=0;
		for(int i=0;i<slide_names.size();i++) {
			if(slide_names[i]=="") continue;
			combine_opt.append(slide_names[i]+".pdf ");
			totslide++;
		}
		call_script("combine-pdf", combine_opt);
		clean_garbage();
		cerr << "Number of slides: " << totslide << "\n";
	}

};

#endif
